/**
 * 添加或修改规则信息
 * 
 * @param {}
 * 
 * @param {}
 *            title
 * @return {}
 */
function saveSalesRlueWin(id, title, isEnable) {
	var salesRuleForm = new Ext.form.FormPanel({
		id : 'salesRuleForm',
		frame : true,
		buttonAlign : 'center',
		monitorValid : true,
		bodyBorder : false,
		fileUpload : true,// 上传文件设置为true
		reader : new Ext.data.JsonReader({
					success : 'success',
					root : 'data'
				}, [{
							name : "id"
						}, {
							name : "ruleName"
						}, {
							name : "ruleDescribe"
						}, {
							name : "ruleType"
						}, {
							name : "discountNumber"
						}, {
							name : "lowLimitMoney"
						}, {
							name : "benefitMoney"
						}, {
							name : "eachFullMoney"
						}, {
							name : "eachFewNumber"
						}, {
							name : "goodsGiftsGroupId"
						}, {
							name : "priority"
						}, {
							name : "isEnable"
						}, {
							name : "createdDate"
						}, {
							name : "createdName"
						}, {
							name : "updatedDate"
						}, {
							name : "updatedName"
						}, {
							name : "limitNumber"
						}]),
		items : [{
			layout : 'column',
			border : false,
			items : [{
				labelWidth : 90,
				columnWidth : .48,
				layout : 'form',
				border : false,
				items : [{
							xtype : 'hidden',
							name : 'id',
							value : id
						}, {
							xtype : 'textfield',
							anchor : '100%',
							labelStyle : "text-align:right;",
							fieldLabel : "<span style='color:#F00'>*</span>规则名称",
							allowBlank : false,
							blankText : '规则名称不能为空',
							name : "ruleName"
						}]
			}, {
				labelWidth : 90,
				columnWidth : .48,
				layout : 'form',
				border : false,
				items : [{
							id : 'salesRuleForm_priority',
							xtype : 'numberfield',
							anchor : '100%',
							labelStyle : "text-align:right;",
							fieldLabel : "<span style='color:#F00'>*</span>优先级",
							allowBlank : false,
							allowDecimals : false,// 禁用小数
							allowNegative : false, // 禁用负数
							minValue : 1,
							blankText : '优先级不能为空',
							name : "priority"
						}]
			}]
		}, {
			layout : 'column',
			border : false,
			items : [{
				labelWidth : 90,
				columnWidth : .48,
				layout : 'form',
				border : false,
				items : [{
					id : 'salesRuleForm_ruleType',
					xtype : 'dataDictionaryCmb',
					anchor : '100%',
					fieldLabel : "<span style='color:#F00'>*</span>规则类型",
					labelStyle : "text-align:right;",
					editable : false,
					autoLoad : true,
					existall : false,
					hiddenName : 'ruleType',
					dictionaryType : PmsUtil.rule_type,
					defaultFirstRow : 1,
					listeners : {
						'select' : function(obj) {
							var ruleType = obj.value;
							var discountNumber = Ext
									.getCmp('salesRuleForm_discountNumber');
							var lowLimitMoney = Ext
									.getCmp('salesRuleForm_lowLimitMoney');
							var eachFullMoney = Ext
									.getCmp('salesRuleForm_eachFullMoney');
							var benefitMoney = Ext
									.getCmp('salesRuleForm_benefitMoney');
							var eachFewNumber = Ext
									.getCmp('salesRuleForm_eachFewNumber');

							var limitNumber = Ext
									.getCmp('salesRuleForm_limitNumber');
								
								// 控制控件的显示和隐藏
								validRuleType(ruleType);

								discountNumber.setValue(null);
								lowLimitMoney.setValue(null);
								eachFullMoney.setValue(null);
								benefitMoney.setValue(null);
								eachFewNumber.setValue(null);
								limitNumber.setValue(null);
							if (ruleType == 0) {
								limitNumber.setValue(1);
							} else if (ruleType == 2) {
								lowLimitMoney.getEl().dom.readOnly = false;
							} else if (ruleType == 3) {
								lowLimitMoney.setValue(0);
								lowLimitMoney.getEl().dom.readOnly = true;
							}
						}
					}
				}]
			}, {
				layout : 'form',
				border : false,
				labelWidth : 90,
				columnWidth : .48,
				items : [{
							id : 'salesRuleForm_isEnable',
							xtype : 'displayfield',
							anchor : '100%',
							fieldLabel : '是否可用',
							labelStyle : "text-align:right;",
							name : "isEnable",
							value : PmsUtil.FixingIsEnable(isEnable)

						}]
			}]
		}, {
			layout : 'column',
			border : false,
			items : [{
				labelWidth : 90,
				columnWidth : .48,
				layout : 'form',
				border : false,
				items : [{
					id : 'salesRuleForm_eachFewNumber',
					xtype : 'numberfield',
					anchor : '100%',
					fieldLabel : "<span style='color:#F00'>*</span>间隔折扣数",
					labelStyle : "text-align:right;",
					allowDecimals : false,// 禁用小数
					allowNegative : false, // 禁用负数
					name : 'eachFewNumber',
					listeners : {
						"afterrender" : function(comp, eOpts) {
							comp.getEl().up('.x-form-item').setDisplayed(false);
						}
					}
				}]
			}, {
				layout : 'form',
				border : false,
				labelWidth : 90,
				columnWidth : .48,
				items : [{
					id : 'salesRuleForm_discountNumber',
					xtype : 'numberfield',
					vtype : 'discountNumberValidate',
					anchor : '100%',
					fieldLabel : "<span style='color:#F00'>*</span>商品折扣",
					labelStyle : "text-align:right;",
					allowNegative : false, // 禁用负数
					name : 'discountNumber',
					decimalPrecision : 1,
					listeners : {
						"afterrender" : function(comp, eOpts) {
							comp.getEl().up('.x-form-item').setDisplayed(false);
						}
					}
				}]
			}]
		}, {
			layout : 'column',
			border : false,
			items : [{
				labelWidth : 90,
				columnWidth : .48,
				layout : 'form',
				border : false,
				items : [{
					id : 'salesRuleForm_eachFullMoney',
					xtype : 'numberfield',
					anchor : '100%',
					fieldLabel : "<span style='color:#F00'>*</span>每满金额",
					labelStyle : "text-align:right;",
					allowDecimals : false,// 禁用小数
					allowNegative : false, // 禁用负数
					name : 'eachFullMoney',
					listeners : {
						"afterrender" : function(comp, eOpts) {
							comp.getEl().up('.x-form-item').setDisplayed(false);
						}
					}
				}]
			}, {
				layout : 'form',
				border : false,
				labelWidth : 90,
				columnWidth : .48,
				items : [{

					id : 'salesRuleForm_lowLimitMoney',
					xtype : 'numberfield',
					anchor : '100%',
					fieldLabel : "<span style='color:#F00'>*</span>最低订单总额",
					labelStyle : "text-align:right;",
					allowNegative : false, // 禁用负数
					name : 'lowLimitMoney',
					decimalPrecision : 2,
					displayed : false,
					listeners : {
						"afterrender" : function(comp, eOpts) {
							comp.getEl().up('.x-form-item').setDisplayed(false);
						}
					}
				}]
			}]

		}, {
			layout : 'column',
			border : false,
			items : [{
				labelWidth : 90,
				columnWidth : .48,
				layout : 'form',
				border : false,
				items : [{
					id : 'salesRuleForm_benefitMoney',
					xtype : 'numberfield',
					anchor : '100%',
					fieldLabel : "<span style='color:#F00'>*</span>优惠金额",
					labelStyle : "text-align:right;",
					// allowDecimals : false,// 禁用小数
					allowNegative : false, // 禁用负数
					name : 'benefitMoney',
					decimalPrecision : 2,
					listeners : {
						"afterrender" : function(comp, eOpts) {
							comp.getEl().up('.x-form-item').setDisplayed(true);
						}
					}
				}]
			}, {
				labelWidth : 90,
				columnWidth : .48,
				layout : 'form',
				border : false,
				items : [{
							id : 'salesRuleForm_limitNumber',
							xtype : 'numberfield',
							anchor : '100%',
							fieldLabel : "<span style='color:#F00'>*</span>限制商品个数",
							labelStyle : "text-align:right;",
							allowDecimals : false,// 禁用小数
							allowNegative : false, // 禁用负数
							allowBlank : false,
							name : 'limitNumber',
							value : 1
						}]
			}]
		}, {
			layout : 'column',
			border : false,
			items : [{
						labelWidth : 90,
						columnWidth : .96,
						layout : 'form',
						border : false,
						items : [{
									xtype : 'textarea',
									anchor : '100%',
									labelStyle : "text-align:right;",
									fieldLabel : "规则描述",
									name : "ruleDescribe"
								}]
					}]
		}],
		buttons : [{
					text : '确定',
					formBind : true,
					handler : function() {
						saveSalesRule();
					}
				}, {
					text : "取消",
					handler : function() {
						saveSalesRlueWindow.close();
					}
				}]
	});
	var saveSalesRlueWindow = new Ext.Window({
				id : 'saveSalesRlueWindow',
				closeAction : 'close',
				title : title,
				closable : true,
				plain : true,
				modal : true,
				width : 640,
				height : 290,
				draggable : false, // 不可拖动
				resizable : false, // 不可调整
				buttonAlign : 'center',
				autoDestroy : true,
				layout : 'fit',
				items : [salesRuleForm],
				listeners : {
					'show' : function() {
						selectSalesRuleById(id);
					},
					'beforeshow' : function() {
						saveSalesRlueWindow.center();
					}
				}
			});
	return saveSalesRlueWindow;

};

/**
 * 查询规则对象
 * 
 * @param {}
 *            id
 */
function selectSalesRuleById(id) {
	if (id != null && id != "undefined") {
		Ext.getCmp("salesRuleForm").getForm().load({
			// deferredRender : false,
			url : __ctxPath + "/couponRuleManager/querySalesRuleById",
			params : {
				id : id
			},
			method : "POST",
			waitTitle : "请稍候",
			waitMsg : "正在载入数据...",
			root : "data",
			success : function(form, action) {
				var data = action.result.data;
				var ruleType = data.ruleType;
				// 设置是否可用
				Ext.getCmp('salesRuleForm_isEnable').setValue(data.isEnable);
				Ext.getCmp('salesRuleForm_isEnable').setValue(PmsUtil
						.FixingIsEnable(data.isEnable));
				validRuleType(ruleType);
			},
			failure : function(f, g) {
				Ext.MessageBox.show({
							title : "操作信息",
							msg : "数据加载失败!",
							buttons : Ext.MessageBox.OK,
							icon : Ext.MessageBox.ERROR
						});
			}
		});
	}
}
/**
 * 添加或修改规则信息
 */
function saveSalesRule() {
	var ruleType = Ext.getCmp('salesRuleForm_ruleType').getValue();
	var eachFullMoney = Ext.getCmp('salesRuleForm_eachFullMoney').getValue();
	var benefitMoney = Ext.getCmp('salesRuleForm_benefitMoney').getValue();
	var lowLimitMoney = Ext.getCmp('salesRuleForm_lowLimitMoney').getValue();

	// 校验有条件减满
	if (ruleType == 2 && benefitMoney > lowLimitMoney) {
		Ext.MessageBox.show({
					title : "操作信息",
					msg : "优惠金额不能大于最低订单总额!",
					buttons : Ext.MessageBox.OK,
					icon : Ext.MessageBox.ERROR
				});
		return;
	}
	// 校验每满递减
	if (ruleType == 4 && benefitMoney > eachFullMoney) {
		Ext.MessageBox.show({
					title : "操作信息",
					msg : "优惠金额不能大于每满金额!",
					buttons : Ext.MessageBox.OK,
					icon : Ext.MessageBox.ERROR
				});
		return;
	}

	var salesRuleForm = Ext.getCmp("salesRuleForm");
	if (salesRuleForm.getForm().isValid()) {
		Ext.Msg.confirm("提示框", "确认要保存规则信息吗?", function(button) {
					if (button == "yes") {
						salesRuleForm.getForm().submit({
							url : __ctxPath
									+ '/couponRuleManager/insertOrUpdateSalesRule',
							waitTitle : "请稍候",
							waitMsg : "正在提交表单，请稍候......",
							failure : function(response, options) {
								var jsonData = Ext.util.JSON
										.decode(options.response.responseText);
								Ext.MessageBox.show({
											title : "错误提示",
											msg : jsonData.data,
											buttons : Ext.MessageBox.OK,
											icon : Ext.MessageBox.ERROR
										});
							},
							success : function(response, options) {
								var jsonData = Ext.util.JSON
										.decode(options.response.responseText);
								Ext.MessageBox.show({
											title : "信息提示",
											msg : jsonData.data,
											buttons : Ext.MessageBox.OK,
											icon : Ext.MessageBox.INFO
										});
								Ext.getCmp("saveSalesRlueWindow").close();
								var gridPanel = Ext.getCmp('ruleManagerGrid');
								if (typeof(gridPanel) != 'undefined') {
									gridPanel.store.reload();
								}
							}
						});
					}
				});
	}
}

// 根据ruleType显示控件
function validRuleType(ruleType) {
	var discountNumber = Ext.getCmp('salesRuleForm_discountNumber');
	var lowLimitMoney = Ext.getCmp('salesRuleForm_lowLimitMoney');
	var eachFullMoney = Ext.getCmp('salesRuleForm_eachFullMoney');
	var benefitMoney = Ext.getCmp('salesRuleForm_benefitMoney');
	var eachFewNumber = Ext.getCmp('salesRuleForm_eachFewNumber');
	var limitNumber = Ext.getCmp('salesRuleForm_limitNumber');

	if (ruleType == 0) {
		discountNumber.getEl().up('.x-form-item').setDisplayed(false);
		lowLimitMoney.getEl().up('.x-form-item').setDisplayed(false);
		eachFullMoney.getEl().up('.x-form-item').setDisplayed(false);
		benefitMoney.getEl().up('.x-form-item').setDisplayed(true);
		eachFewNumber.getEl().up('.x-form-item').setDisplayed(false);
		limitNumber.getEl().up('.x-form-item').setDisplayed(true);

		discountNumber.allowBlank = true;
		lowLimitMoney.allowBlank = true;
		eachFullMoney.allowBlank = true;
		benefitMoney.allowBlank = false;
		eachFewNumber.allowBlank = true;
		limitNumber.allowBlank = false;
	} else if (ruleType == 1) {
		discountNumber.getEl().up('.x-form-item').setDisplayed(true);
		lowLimitMoney.getEl().up('.x-form-item').setDisplayed(false);
		eachFullMoney.getEl().up('.x-form-item').setDisplayed(false);
		benefitMoney.getEl().up('.x-form-item').setDisplayed(false);
		eachFewNumber.getEl().up('.x-form-item').setDisplayed(false);
		limitNumber.getEl().up('.x-form-item').setDisplayed(false);
		discountNumber.allowBlank = false;
		lowLimitMoney.allowBlank = true;
		eachFullMoney.allowBlank = true;
		benefitMoney.allowBlank = true;
		eachFewNumber.allowBlank = true;
		limitNumber.allowBlank = true;
	} else if (ruleType == 2) {
		discountNumber.getEl().up('.x-form-item').setDisplayed(false);
		lowLimitMoney.getEl().up('.x-form-item').setDisplayed(true);
		eachFullMoney.getEl().up('.x-form-item').setDisplayed(false);
		benefitMoney.getEl().up('.x-form-item').setDisplayed(true);
		eachFewNumber.getEl().up('.x-form-item').setDisplayed(false);
		limitNumber.getEl().up('.x-form-item').setDisplayed(false);

		discountNumber.allowBlank = true;
		lowLimitMoney.allowBlank = false;
		eachFullMoney.allowBlank = true;
		benefitMoney.allowBlank = false;
		eachFewNumber.allowBlank = true;
		limitNumber.allowBlank = true;
		lowLimitMoney.getEl().dom.readOnly = false;
	} else if (ruleType == 3) {
		discountNumber.getEl().up('.x-form-item').setDisplayed(false);
		lowLimitMoney.getEl().up('.x-form-item').setDisplayed(true);
		eachFullMoney.getEl().up('.x-form-item').setDisplayed(false);
		benefitMoney.getEl().up('.x-form-item').setDisplayed(true);
		eachFewNumber.getEl().up('.x-form-item').setDisplayed(false);
		limitNumber.getEl().up('.x-form-item').setDisplayed(false);

		discountNumber.allowBlank = true;
		lowLimitMoney.allowBlank = false;
		eachFullMoney.allowBlank = true;
		benefitMoney.allowBlank = false;
		eachFewNumber.allowBlank = true;
		limitNumber.allowBlank = true;
		lowLimitMoney.getEl().dom.readOnly = true;
	} else if (ruleType == 4) {
		discountNumber.getEl().up('.x-form-item').setDisplayed(false);
		lowLimitMoney.getEl().up('.x-form-item').setDisplayed(false);
		eachFullMoney.getEl().up('.x-form-item').setDisplayed(true);
		benefitMoney.getEl().up('.x-form-item').setDisplayed(true);
		eachFewNumber.getEl().up('.x-form-item').setDisplayed(false);
		limitNumber.getEl().up('.x-form-item').setDisplayed(false);

		discountNumber.allowBlank = true;
		lowLimitMoney.allowBlank = true;
		eachFullMoney.allowBlank = false;
		benefitMoney.allowBlank = false;
		eachFewNumber.allowBlank = true;
		limitNumber.allowBlank = true;
	} else if (ruleType == 5) {
		discountNumber.getEl().up('.x-form-item').setDisplayed(true);
		lowLimitMoney.getEl().up('.x-form-item').setDisplayed(false);
		eachFullMoney.getEl().up('.x-form-item').setDisplayed(false);
		benefitMoney.getEl().up('.x-form-item').setDisplayed(false);
		eachFewNumber.getEl().up('.x-form-item').setDisplayed(true);
		limitNumber.getEl().up('.x-form-item').setDisplayed(false);

		discountNumber.allowBlank = false;
		lowLimitMoney.allowBlank = true;
		eachFullMoney.allowBlank = true;
		benefitMoney.allowBlank = true;
		eachFewNumber.allowBlank = false;
		limitNumber.allowBlank = true;
	}
}